package controller.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

//过滤器解决全局乱码问题
@WebFilter("/*")
public class FilterEncoding implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        if (request.getMethod().equalsIgnoreCase("post")) {
            request.setCharacterEncoding("utf-8");
        }
        filterChain.doFilter(request, servletResponse);
    }
    @Override
    public void destroy() {

    }
}
